package com.gitee.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mapper.OrderMapper;
import com.gitee.pojo.Order;
import com.gitee.service.IOrderService;
import com.gitee.vo.OrderVo;
import com.gitee.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 订单功能
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PageVo<OrderVo> pageOrderVos(String eatTime, Integer cookbookType,
                                        Integer orderStatus, Integer orderType,
                                        Integer pageNum, Integer pageSize) {

        List<OrderVo> orderVos = orderMapper.selectOrderVos((pageNum - 1) * pageSize, pageSize, eatTime, cookbookType, orderStatus, orderType);
        LambdaQueryWrapper<Order> queryWrapper = new QueryWrapper<Order>().lambda()
                .eq(!StrUtil.isBlank(eatTime), Order::getEatTime, eatTime)
                .eq(cookbookType != null, Order::getCookbookType, cookbookType)
                .eq(orderStatus != null, Order::getOrderStatus, orderStatus)
                .eq(orderType != null, Order::getOrderType, orderType);
        Integer count = super.count(queryWrapper);
        return new PageVo<>(orderVos, count);
    }

    @Override
    public List<Order> listByUserId(Integer userId) {
        LambdaQueryWrapper<Order> queryWrapper = new QueryWrapper<Order>().lambda().eq(Order::getUserId, userId);
        return super.list(queryWrapper);
    }

    @Override
    public List<Order> listByUserIds(Integer[] userIds) {
        LambdaQueryWrapper<Order> queryWrapper = new QueryWrapper<Order>().lambda().in(Order::getUserId, Arrays.asList(userIds));
        return super.list(queryWrapper);
    }
}
